package com.home.base.core.framework.msg;

import com.home.base.core.framework.enums.ResultCodeEnum;

import java.io.Serializable;

/**
 * Description: 业务操作返回结果集
 * Created by 王大宸 on 2020-09-23 0:35
 * Created with IntelliJ IDEA.
 */
public class ServerResponse<T> implements Serializable {
    private static final long serialVersionUID = -8089808987807345969L;

    /**
     * 是否成功
     */
    private Boolean success;

    /**
     * 状态:
     */
    private int status;

    /**
     * 消息
     */
    private String msg;

    /**
     * 数据
     */
    private T data;


    private ServerResponse() {
    }

    private ServerResponse(Boolean success, int status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
        this.success = success;
    }

    /**
     * 操作成功
     */
    public static <T> ServerResponse<T> ok() {
        return new ServerResponse<T>(ResultCodeEnum.SUCCESS.getSuccess(), ResultCodeEnum.SUCCESS.getStatus(), ResultCodeEnum.SUCCESS.getMessage(), null);
    }

    public static <T> ServerResponse<T> okMsg(String msg) {
        return new ServerResponse<T>(ResultCodeEnum.SUCCESS.getSuccess(), ResultCodeEnum.SUCCESS.getStatus(), msg, null);
    }

    public static <T> ServerResponse<T> okData(T data) {
        return new ServerResponse<T>(ResultCodeEnum.SUCCESS.getSuccess(), ResultCodeEnum.SUCCESS.getStatus(), ResultCodeEnum.SUCCESS.getMessage(), data);
    }

    public static <T> ServerResponse<T> okMsgData(String msg, T data) {
        return new ServerResponse<T>(ResultCodeEnum.SUCCESS.getSuccess(), ResultCodeEnum.SUCCESS.getStatus(), msg, data);
    }

    public static <T> ServerResponse<T> okStatusMsg(int status, String msg) {
        return new ServerResponse<T>(ResultCodeEnum.SUCCESS.getSuccess(), ResultCodeEnum.SUCCESS.getStatus(), msg, null);
    }

    public static <T> ServerResponse<T> okStatusMsgDate(int status, String msg, T data) {
        return new ServerResponse<T>(ResultCodeEnum.SUCCESS.getSuccess(), status, msg, data);
    }

    /**
     * 操作错误
     */
    public static <T> ServerResponse<T> err() {
        return new ServerResponse<T>(ResultCodeEnum.ERROR.getSuccess(), ResultCodeEnum.ERROR.getStatus(), ResultCodeEnum.ERROR.getMessage(), null);
    }

    public static <T> ServerResponse<T> errMsg(String msg) {
        return new ServerResponse<T>(ResultCodeEnum.ERROR.getSuccess(), ResultCodeEnum.ERROR.getStatus(), msg, null);
    }

    public static <T> ServerResponse<T> errData(T data) {
        return new ServerResponse<T>(ResultCodeEnum.ERROR.getSuccess(), ResultCodeEnum.ERROR.getStatus(), ResultCodeEnum.ERROR.getMessage(), data);
    }

    public static <T> ServerResponse<T> errMsgData(String msg, T data) {
        return new ServerResponse<T>(ResultCodeEnum.ERROR.getSuccess(), ResultCodeEnum.ERROR.getStatus(), msg, data);
    }

    public static <T> ServerResponse<T> errStatusMsg(int status, String msg) {
        return new ServerResponse<T>(ResultCodeEnum.ERROR.getSuccess(), status, msg, null);
    }

    public static <T> ServerResponse<T> errStatus(int status) {
        return new ServerResponse<T>(ResultCodeEnum.ERROR.getSuccess(), status, null, null);
    }

    public static <T> ServerResponse<T> errStatusMsgDate(int status, String msg, T data) {
        return new ServerResponse<T>(ResultCodeEnum.ERROR.getSuccess(), status, msg, data);
    }

    /**
     * 系统异常
     */
    public static <T> ServerResponse<T> ex() {
        return new ServerResponse<T>(ResultCodeEnum.EXCEPTION.getSuccess(), ResultCodeEnum.EXCEPTION.getStatus(), ResultCodeEnum.EXCEPTION.getMessage(), null);
    }

    public static <T> ServerResponse<T> exDate(T data) {
        return new ServerResponse<T>(ResultCodeEnum.EXCEPTION.getSuccess(), ResultCodeEnum.EXCEPTION.getStatus(), ResultCodeEnum.EXCEPTION.getMessage(), data);
    }

    public static <T> ServerResponse<T> exMsg(String msg) {
        return new ServerResponse<T>(ResultCodeEnum.EXCEPTION.getSuccess(), ResultCodeEnum.EXCEPTION.getStatus(), msg, null);
    }

    public static <T> ServerResponse<T> exDateMsg(T data, String msg) {
        return new ServerResponse<T>(ResultCodeEnum.EXCEPTION.getSuccess(), ResultCodeEnum.EXCEPTION.getStatus(), msg, data);
    }

    public static <T> ServerResponse<T> exStatusMsg(int status, String msg) {
        return new ServerResponse<T>(ResultCodeEnum.EXCEPTION.getSuccess(), status, msg, null);
    }

    public static <T> ServerResponse<T> exStatusDataMsg(int status, String msg, T data) {
        return new ServerResponse<T>(ResultCodeEnum.EXCEPTION.getSuccess(), status, msg, data);
    }

    /**
     * 参数校验错误
     */
    public static <T> ServerResponse<T> valid(T data, String msg) {
        return new ServerResponse<T>(ResultCodeEnum.PARAMS_VALID.getSuccess(), ResultCodeEnum.PARAMS_VALID.getStatus(), msg, data);
    }

    /**
     * 参数校验错误,自定义返回消息
     */
    public static <T> ServerResponse<T> validMsg(String msg) {
        return new ServerResponse<T>(ResultCodeEnum.PARAMS_VALID_MSG.getSuccess(), ResultCodeEnum.PARAMS_VALID_MSG.getStatus(), msg, null);
    }

    public static <T> ServerResponse<T> statusMsg(int status, String msg) {
        return new ServerResponse<T>(ResultCodeEnum.EXCEPTION.getSuccess(), status, msg, null);
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


}
